package com.bds.snatch.system.task;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;

import com.bds.snatch.model.H2Urls;
import com.bds.snatch.model.Task;
import com.bds.snatch.model.TemplateUrl;
import com.bds.snatch.system.queue.TQueue;
import com.bds.snatch.system.util.SpringContextHolder;

public class UrlTask extends Thread {
	private static Logger logger = Logger.getLogger(UrlTask.class);
	private Dao h2dao;

	public UrlTask(Dao h2dao) {
		this.h2dao = h2dao;
	}

	@Override
	public void run() {
		NutDao apiDao = SpringContextHolder.getBean("dao");

		List<TemplateUrl> list = apiDao.query(TemplateUrl.class,
				Cnd.where("status", "=", 0).and("send_status", "=", 0));

		for (TemplateUrl url : list) {
			H2Urls urls = new H2Urls();
			urls.setId(url.getId());
			urls.setUser_id(url.getUser_id());
			urls.setRequest_id(url.getRequest_id());
			urls.setUrl(url.getUrl());
			urls.setIsjs("0");
			urls.setCreate_time(new Date());
			urls.setType("url");
			TQueue.getQueue().add(urls);
			try {
				h2dao.fastInsert(urls);
				apiDao.update(TemplateUrl.class, Chain.make("send_status", 2), Cnd.where("id", "=", url.getId()));
			} catch (Exception e) {
				logger.error("", e);
			}

		}

	}
}
